<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of sgf</title>
  <meta name="keywords" content="sgf">
  <meta name="description" content="sgf --- simple graph format: a 2-column matrix representation">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../index.html">Home</a> &gt;  <a href="../index.html">matgraph</a> &gt; <a href="index.html">@graph</a> &gt; sgf.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../index.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for matgraph/@graph&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>sgf
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>sgf --- simple graph format: a 2-column matrix representation</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>function A = sgf(g,M) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment"> sgf --- simple graph format: a 2-column matrix representation
 A = sgf(g) --- make the simple graph format matrix of g
 sgf(g,M)   --- overwrite g with the graph specified in M

 The simple graph format is a 2-column matrix representation of a graph.
 This format optionally includes embedding information.
 If A is the SGF of a graph, the rows of A are as follows:

 * First row is [n m] where n is the number of vertices and m is the number
   of edges.
 * Rows 2 through m+1 specify the edges of the graph.
 * Optionally, rows (m+1)+1 through (m+1)+n give the x,y-coordinates of
   the vertices.</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="add.html" class="code" title="function add(g,i,j)">add</a>	add --- add edge(s) to the graph</li><li><a href="edges.html" class="code" title="function elist = edges(g)">edges</a>	edges(g) --- list the edges in g as a 2-column matrix</li><li><a href="embed.html" class="code" title="function embed(g,xy)">embed</a>	embed --- create an embedding for a graph</li><li><a href="full.html" class="code" title="function full(g)">full</a>	full(g) --- convert internal storage for g to full</li><li><a href="getxy.html" class="code" title="function z = getxy(g)">getxy</a>	getxy(g) --- give g's embedding (or [] if g doesn't have one)</li><li><a href="hasxy.html" class="code" title="function yn = hasxy(g)">hasxy</a>	hasxy(g) --- determine if an embedding has been created for g</li><li><a href="ne.html" class="code" title="function m = ne(g,h)">ne</a>	ne(g) --- number of edges in g or ne(g,h) --- check for inequality</li><li><a href="nv.html" class="code" title="function n = nv(g)">nv</a>	nv(g) --- number of vertices in g</li><li><a href="resize.html" class="code" title="function resize(g, n)">resize</a>	resize(g,n) --- change the number of vertices in g to n</li><li><a href="size.html" class="code" title="function [sz,tz] = size(g)">size</a>	size(g) --- returns [nv,ne] for the graph</li><li><a href="sparse.html" class="code" title="function sparse(g)">sparse</a>	sparse(g) --- convert internal storage for g to sparse</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
</ul>
<!-- crossreference -->

<h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="#_sub1" class="code">function A = sgf_make(g)</a></li><li><a href="#_sub2" class="code">function ok = sgf_check(M)</a></li></ul>
<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function A = sgf(g,M)</a>
0002 <span class="comment">% sgf --- simple graph format: a 2-column matrix representation</span>
0003 <span class="comment">% A = sgf(g) --- make the simple graph format matrix of g</span>
0004 <span class="comment">% sgf(g,M)   --- overwrite g with the graph specified in M</span>
0005 <span class="comment">%</span>
0006 <span class="comment">% The simple graph format is a 2-column matrix representation of a graph.</span>
0007 <span class="comment">% This format optionally includes embedding information.</span>
0008 <span class="comment">% If A is the SGF of a graph, the rows of A are as follows:</span>
0009 <span class="comment">%</span>
0010 <span class="comment">% * First row is [n m] where n is the number of vertices and m is the number</span>
0011 <span class="comment">%   of edges.</span>
0012 <span class="comment">% * Rows 2 through m+1 specify the edges of the graph.</span>
0013 <span class="comment">% * Optionally, rows (m+1)+1 through (m+1)+n give the x,y-coordinates of</span>
0014 <span class="comment">%   the vertices.</span>
0015 
0016 <span class="keyword">if</span> nargin==1
0017     A = <a href="#_sub1" class="code" title="subfunction A = sgf_make(g)">sgf_make</a>(g);
0018     <span class="keyword">return</span>
0019 <span class="keyword">end</span>
0020     
0021 <span class="keyword">if</span> ~<a href="#_sub2" class="code" title="subfunction ok = sgf_check(M)">sgf_check</a>(M)
0022     error(<span class="string">'Matrix is not a properly formed sgf matrix'</span>);
0023 <span class="keyword">end</span>
0024  
0025 
0026 n = M(1,1);
0027 m = M(1,2);
0028 
0029 <a href="resize.html" class="code" title="function resize(g, n)">resize</a>(g,0);
0030 
0031 <span class="keyword">if</span> n&gt;set_large
0032     <a href="sparse.html" class="code" title="function sparse(g)">sparse</a>(g);
0033 <span class="keyword">else</span>
0034     <a href="full.html" class="code" title="function full(g)">full</a>(g);
0035 <span class="keyword">end</span>
0036 
0037 <a href="resize.html" class="code" title="function resize(g, n)">resize</a>(g,n);
0038 
0039 <a href="edges.html" class="code" title="function elist = edges(g)">edges</a> = M(2:m+1,:);
0040 <a href="add.html" class="code" title="function add(g,i,j)">add</a>(g,<a href="edges.html" class="code" title="function elist = edges(g)">edges</a>);
0041 
0042 <span class="comment">% see if there's an embedding</span>
0043 [r,c] = <a href="size.html" class="code" title="function [sz,tz] = size(g)">size</a>(M);
0044 <span class="keyword">if</span> (r&gt;m+1)
0045     xy = M(m+2:<span class="keyword">end</span>,:);
0046     <a href="embed.html" class="code" title="function embed(g,xy)">embed</a>(g,xy);
0047 <span class="keyword">end</span>
0048 
0049 
0050 
0051 <a name="_sub1" href="#_subfunctions" class="code">function A = sgf_make(g)</a>
0052 <span class="comment">% helper function to convert a graph to a sgf matrix</span>
0053 n = <a href="nv.html" class="code" title="function n = nv(g)">nv</a>(g);
0054 m = <a href="ne.html" class="code" title="function m = ne(g,h)">ne</a>(g);
0055 <span class="keyword">if</span> <a href="hasxy.html" class="code" title="function yn = hasxy(g)">hasxy</a>(g)
0056     A = zeros(n+m+1,2);
0057 <span class="keyword">else</span>
0058     A = zeros(m+1,2);
0059 <span class="keyword">end</span>
0060 
0061 A(1,:) = [n,m];
0062 A(2:m+1,:) = <a href="edges.html" class="code" title="function elist = edges(g)">edges</a>(g);
0063 <span class="keyword">if</span> <a href="hasxy.html" class="code" title="function yn = hasxy(g)">hasxy</a>(g)
0064     A(m+2:<span class="keyword">end</span>,:) = <a href="getxy.html" class="code" title="function z = getxy(g)">getxy</a>(g);
0065 <span class="keyword">end</span>
0066 
0067 <a name="_sub2" href="#_subfunctions" class="code">function ok = sgf_check(M)</a>
0068 <span class="comment">% check a matrix to be sure it's a valid sgf</span>
0069 
0070 ok = false;
0071 
0072 [r,c] = <a href="size.html" class="code" title="function [sz,tz] = size(g)">size</a>(M);
0073 <span class="keyword">if</span> (c ~= 2) || (r==0)
0074     <span class="keyword">return</span>
0075 <span class="keyword">end</span>
0076 
0077 row1 = M(1,:);
0078 n = row1(1);
0079 m = row1(2);
0080 
0081 <span class="keyword">if</span> (n &lt; 0) || (m &lt; 0)
0082     <span class="keyword">return</span>
0083 <span class="keyword">end</span>
0084 
0085 <span class="keyword">if</span> ~((r==m+1) || (r==m+n+1))
0086     <span class="keyword">return</span>
0087 <span class="keyword">end</span>
0088 
0089 <a href="edges.html" class="code" title="function elist = edges(g)">edges</a> = M(2:m+1,:);
0090 <span class="comment">% make sure they are all integers</span>
0091 <span class="keyword">if</span> any(any(<a href="edges.html" class="code" title="function elist = edges(g)">edges</a> ~= round(<a href="edges.html" class="code" title="function elist = edges(g)">edges</a>)))
0092     <span class="keyword">return</span>
0093 <span class="keyword">end</span>
0094 <span class="comment">% make sure they are all in {1,2,...,n}</span>
0095 <span class="keyword">if</span> any(any(<a href="edges.html" class="code" title="function elist = edges(g)">edges</a> &lt; 1)) || any(any(<a href="edges.html" class="code" title="function elist = edges(g)">edges</a>&gt;n))
0096     <span class="keyword">return</span>
0097 <span class="keyword">end</span>
0098 <span class="comment">% make sure there are no loops</span>
0099 <span class="keyword">if</span> any(<a href="edges.html" class="code" title="function elist = edges(g)">edges</a>(:,1)==<a href="edges.html" class="code" title="function elist = edges(g)">edges</a>(:,2))
0100     <span class="keyword">return</span>
0101 <span class="keyword">end</span>
0102 
0103 <span class="comment">% all tests passed</span>
0104     
0105 ok = true;</pre></div>
<hr><address>Generated on Fri 30-Apr-2010 07:51:16 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>